package com.lkd.handler;


import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

//处理自动维护工单



@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
public class VMStatusHandler  implements MsgHandler {
    @Autowired
    private VMService vmService;
    @Autowired
    private TaskService  taskService;
    @Override
    public void process(String jsonMsg) throws IOException {
        //解码
        VmStatusContract vmStatusContract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        if(vmStatusContract==null||vmStatusContract.getStatusInfo()==null||vmStatusContract.getStatusInfo().size()<0)
        {
            return;
        }
        //如果状态为false则创建维修工单
     if( vmStatusContract.getStatusInfo().stream().anyMatch(s->(
             !s.isStatus()
     ))){
         TaskViewModel tvm = new TaskViewModel();
         //根据售货机编号查询售货机
         VmVO vmInfo = vmService.getVMInfo(vmStatusContract.getInnerCode());
         //获取工单任务最少的运维人员
      var  userid=  taskService.getLeastUser(vmInfo.getRegionId(),true);
      //创建工单
      tvm.setUserId(userid);
      tvm.setInnerCode(vmStatusContract.getInnerCode());
      tvm.setCreateType(0);
      tvm.setDesc("自动维修工单");
         taskService.productionOrderSave(tvm);
        }

    }
}
